#_*_ coding:utf-8 _*_
import os
file =os.listdir(".")

def file_cpy(a_file,b_file):
    "拷贝文件内容，支持多行，从args1到args2"
    src = open(a_file, "w")
    li = ['hello world\n', 'hello China\n']
    if os.path.exists(a_file):
        print("文件已经存在，先销毁在写入")
        src.close()
    print("新建立1个文件")
    src = open(a_file, "w")
    src.writelines(li)
    src.close()

    src =open(a_file,'r')
    dst =open(b_file,'w')
    dst.writelines(src.readlines()) #写2条信息要写成readlines
    src.close()
    dst.close()

#用法：file_cpy("test.txt","test4.txt")

def rename_end(oldfile,newfile):
    "修改文件的后缀 oldfile路径修改到newfile"
    for filename in file:
        pos =filename.find(".")

        if filename [pos+1:] ==oldfile: #pos等于.pos+1等于.后面的部分
            newname =filename[:pos+1]+newfile
            os.rename(filename,newname)
rename_end("html","htm")

def mk_file(path,testText):
    #txt = testText
    "当前目录下面创建文件的函数mk_file"
    src =open(path,"w")
    # src.writelines(txt)
    if os.path.exists(path):
        print("文件已存在，先删除")
        src.close() #判断有后，先删除
        os.remove(path)
        print("文件已存在，开始写入")
        src1 = open(path, "w")
        src1.write(testText)
        src1.close()
#目前不支持写字典
mk_file("test1.txt","i love python")